(in-package :gwee)

(defclass divider (view background-mixin foreground-mixin margin-mixin)
  ((direction :type (member :vertical :horizontal) :initform :vertical :initarg :direction :accessor divider-direction))
  (:documentation "A horizontal or vertical divider in the form of a simple line.")
  (:default-initargs
      :border-color nil
      :margin 3))

(defun divider-update-hints (view)
  (ecase (divider-direction view)
    (:vertical
     (setf (best-width view) :minimum
           (best-height view) :maximum))
    (:horizontal
     (setf (best-width view) :maximum
           (best-height view) :minimum))))

(defmethod shared-initialize :after ((view divider) slots &key)
  (divider-update-hints view))

(defmethod (setf divider-direction) :after (val (view divider))
  (divider-update-hints view))

(defmethod draw-view ((view divider))
  (ecase (divider-direction view)
    (:vertical
     (draw-line view
                (margin-left view) (margin-top view)
                (margin-left view) (- (view-height view) (margin-bottom view))))
    (:horizontal
     (draw-line view
                (margin-left view) (margin-top view)
                (- (view-width view) (margin-right view)) (margin-top view)))))

(defmethod synchronize-width-minimum ((view divider) (parent view) width height)
  (if (eq (divider-direction view) :horizontal)
      width
      (+ (margin-left view) 1 (margin-right view))))

(defmethod synchronize-height-minimum ((view divider) (parent view) width height)
  (if (eq (divider-direction view) :horizontal)
      (+ (margin-top view) 1 (margin-bottom view))
      height))
